package cn.livingCloud.service.impl;

import cn.livingCloud.entity.content.AbstractReplyContent;
import cn.livingCloud.entity.content.ReplyRule;
import cn.livingCloud.repository.ArticleReplyMapper;
import cn.livingCloud.repository.ReplyRuleMapper;
import cn.livingCloud.service.AutoReplyService;
import cn.livingCloud.service.impl.operation.MemberCreater;
import cn.livingCloud.service.impl.operation.MemberUpdater;
import cn.livingCloud.service.impl.operation.RespMsgConverter;
import cn.livingCloud.wechat.protocol.req.*;
import cn.livingCloud.wechat.protocol.resp.AbstractRespMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * 自动回复
 * Created by sujiantang on 2015/12/11.
 */
@Service
public class AutoReplyServiceImpl implements AutoReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private ReplyRuleMapper ruleMapper;

    @SuppressWarnings("unchecked")
    @Override
    public <T extends AbstractRespMsg> T textHandler(TextMsg textMsg) {
        String content1 = textMsg.getContent();
        String s = null;
        try {
            s = new String(content1.getBytes("GBK"), Charset.forName("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(s + "==============================");
        ReplyRule replyRuleByContent = ruleMapper.getReplyRuleByContent(s);
        if (replyRuleByContent == null) {
            return null;
        }
        AbstractReplyContent content = replyRuleByContent.getContent();
        RespMsgConverter convert = new RespMsgConverter(content, textMsg);
        return (T) convert.convert();
    }

    @Override
    public <T extends AbstractRespMsg> T imageHandler(ImageMsg imageMsg) {
        throw new UnsupportedOperationException("图片回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T voiceHandler(VoiceMsg reqMsg) {
        throw new UnsupportedOperationException("语音回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T videoHandler(VideoMsg reqMsg) {
        throw new UnsupportedOperationException("视频回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T shortvideoHandler(ShortvideoMsg shortvideoMsg) {
        throw new UnsupportedOperationException("短视频回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T locationHandler(LocationMsg locationMsg) {
        throw new UnsupportedOperationException("定位回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T linkHandler(LinkMsg linkMsg) {
        throw new UnsupportedOperationException("链接回复功能未实现");
    }

    @Override
    public <T extends AbstractRespMsg> T subscribePushHandler(SubscribePushMsg subscribePushMsg) {
        MemberCreater creater = new MemberCreater(subscribePushMsg);
        creater.create();

        ReplyRule replyRuleByContent = ruleMapper.getReplyRuleByContent(subscribePushMsg.getEvent().toString());
        if (replyRuleByContent == null) {
            return null;
        }
        AbstractReplyContent content = replyRuleByContent.getContent();
        RespMsgConverter convert = new RespMsgConverter(content, subscribePushMsg);
        return (T) convert.convert();
    }

    @Override
    public <T extends AbstractRespMsg> T unSubscribePushHandler(UnSubscribePushMsg unSubscribePushMsg) {
        MemberUpdater updater = new MemberUpdater();
        updater.updateUnSubscribe(unSubscribePushMsg);
        return null;
    }

    @Override
    public <T extends AbstractRespMsg> T locationPushHandler(LocationPushMsg locationPushMsg) {
        throw new UnsupportedOperationException("功能未实现");
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends AbstractRespMsg> T clickPushHandler(ClickPushMsg clickPushMsg) {
        ReplyRule replyRuleByContent = ruleMapper.getReplyRuleByContent(clickPushMsg.getEventKey());
        if (replyRuleByContent == null) {
            return null;
        }
        AbstractReplyContent content = replyRuleByContent.getContent();
        RespMsgConverter convert = new RespMsgConverter(content, clickPushMsg);
        return (T) convert.convert();
    }

    @Override
    public <T extends AbstractRespMsg> T viewPushHandler(ViewPushMsg viewPushMsg) {
        return null;
    }

    @Override
    public <T extends AbstractRespMsg> T transferCustomService(AbstractReqMsg abstractReqMsg) {
        return null;
    }
}
